package _map

import "container/list"

const base = 911

type pair struct {
	key   int
	value int
}

type MyHashMap struct {
	data []list.List
}

func Constructor() MyHashMap {
	return MyHashMap{data: make([]list.List, base)}
}

func (m MyHashMap) hash(key int) int {
	return key % base
}

func (m *MyHashMap) Put(key int, value int) {
	index := m.hash(key)
	for i := m.data[index].Front(); i != nil; i = i.Next() {
		if key == i.Value.(pair).key {
			i.Value = pair{key, value}
			return
		}
	}
	m.data[index].PushBack(pair{key, value})
}

func (m *MyHashMap) Get(key int) int {
	index := m.hash(key)
	for i := m.data[index].Front(); i != nil; i = i.Next() {
		if key == i.Value.(pair).key {
			return i.Value.(pair).value
		}
	}
	return -1
}

func (m *MyHashMap) Remove(key int) {
	index := m.hash(key)
	for i := m.data[index].Front(); i != nil; i = i.Next() {
		if key == i.Value.(pair).key {
			m.data[index].Remove(i)
		}
	}
}
